<div class="row">
	<div class="col-md-12">
		<ol class="breadcrumb">
			<li>
				<a href="#" onclick="toHome()">
					主页
				</a>
			</li>
			<li>
				<a href="#" class="toList" onclick="toCouponList()">
					通兑卡列表
				</a>
			</li>
			<li class="active">
				添加通兑卡
			</li>
		</ol>
	</div>
</div>
<form role="form" class="form-horizontal">
	<div class="row page-cont">
		<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 cont-top">
			<div class="panel panel-white">
				<div class="panel-heading">
					<h4 class="panel-title">
						<span class="title-text">通兑卡录入</span>
					</h4>
					<div class="panel-tools">
						<div class="dropdown">
							<a data-toggle="dropdown" class="btn btn-xs dropdown-toggle btn-transparent-grey">
								<i class="fa fa-cog"></i>
							</a>
							<ul class="dropdown-menu dropdown-light pull-right" role="menu">
								<li>
									<a class="panel-collapse collapses" href="#"><i class="fa fa-angle-up"></i> <span>折叠</span> </a>
								</li>
								<li>
									<a class="panel-expand" href="#">
										<i class="fa fa-expand"></i> <span>全屏</span>
									</a>
								</li>
							</ul>
						</div>
						<a class="btn btn-xs btn-link panel-close" href="#">
							<i class="fa fa-times"></i>
						</a>
					</div>
				</div>
				<div class="panel-body">
					<!-- <div class="form-group panel-cont">
					    <label class="col-sm-2 control-label">生成卡数量</label>
					    <div class="col-sm-5">
					      <input type="text" class="form-control" id="cardCode" />
					    </div>
					    <span class="col-sm-4 tips">
					    	<i class="fa fa-info-circle"></i>
					    	请输入数字。注：1分钟内只能生成1000张
					    </span>
				  	</div> -->
					
					<div class="body-cont">
						<div class="newCard">
							<!-- <hr style="border-color: #555" /> -->
							<!-- <div class="form-group">
						  		<div class="col-sm-2 col-sm-offset-3">
						  			<button type="button" class="btn btn-green delete-btn">删除</button>
						  		</div>
						  	</div> -->
						  	<div class="form-group panel-cont">
							    <label class="col-sm-2 control-label">优惠券名称</label>
							    <div class="col-sm-5">
							      <input type="text" class="form-control couponName" />
							    </div>
						  	</div>
						  	<div class="form-group panel-cont">
							    <label class="col-sm-2 control-label">优惠券适用场景</label>
							    <div class="col-sm-5">
									<select name="" class="applyScene">
										<option value="REDBAG_">红包</option>
										<option value="EXCHANGE_">通兑券</option>
									</select>
							    </div>
						  	</div>
							<!-- <div class="form-group panel-cont">
							    <label class="col-sm-2 control-label">优惠券数量</label>
							    <div class="col-sm-5">
							      <input type="text" class="form-control" id="couponNum" />
							    </div>
							    <span class="col-sm-4 tips">
							    	<i class="fa fa-info-circle"></i>
							    	请输入数字
							    </span>
						  	</div> -->
						  	<div class="form-group panel-cont">
							    <label class="col-sm-2 control-label">介绍</label>
							    <div class="col-sm-5">
							      <input type="text" class="form-control couponIntro" />
							    </div>
						  	</div>
						  	<div class="form-group panel-cont">
							    <label class="col-sm-2 control-label">开始日期</label>
							    <div class="col-sm-5">
							      <input type="text" class="form-control startDate" />
							    </div>
						  	</div>
						  	<div class="form-group panel-cont">
							    <label class="col-sm-2 control-label">结束日期</label>
							    <div class="col-sm-5">
							      <input type="text" class="form-control endDate" />
							    </div>
						  	</div>
						  	<div class="form-group panel-cont">
							    <label class="col-sm-2 control-label">最大价格</label>
							    <div class="col-sm-5">
							      	<input type="text" class="form-control maxPrice" />
							    </div>
							    <span class="col-sm-4 tips">
							    	<i class="fa fa-info-circle"></i>
							    	红包减免最大金额
							    </span>
						  	</div>
						  	<!-- <div class="form-group panel-cont">
							    <label class="col-sm-2 control-label">最小价格</label>
							    <div class="col-sm-5">
							      	<input type="text" class="form-control" id="minPrice" />
							    </div>
							    <span class="col-sm-4 tips">
							    	<i class="fa fa-info-circle"></i>
							    	支付金额要大于此输入数值才能够使用
							    </span>
						  	</div> -->

						  	<div class="form-group panel-cont">
							    <label class="col-sm-2 control-label">是否限制影片使用</label>
							    <div class="col-sm-5">
							      <input style="margin-top: 8px" class="isShow" type="checkbox" value="0" />
							    </div>
							    <span class="col-sm-4 tips">
							    	<i class="fa fa-info-circle"></i>
							    	若选中，则可填写具体影片
							    </span>
						  	</div>
						  	<div class="hide-text">
						  		<div class="form-group panel-cont">
								    <label class="col-sm-2 control-label">请填写待搜索影片名</label>
								    <div class="col-sm-5">
								    	<input type='text' class='form-control check-film' name='' value='' />
								    </div>
							  	</div>
							  	<div class="form-group panel-cont">
									<label class="col-sm-2 control-label"></label>
								    <div class="col-sm-5">
								    	<ul class="check-film-list">
								    		
								    	</ul>
								    </div>							  		
							  	</div>
						  		<div class="form-group panel-cont">
								    <label class="col-sm-2 control-label">确定影片名</label>
								    <div class="col-sm-5">
								    	<textarea class="form-control movieName" rows="4" cols="12" readonly="readonly"></textarea>
								    </div>
								    <span class="col-sm-4 tips">
								    	<i class="fa fa-info-circle"></i>
								    	提交影片以此栏内容为准
								    </span>
							  	</div>
						  	</div>
						  	<!-- <div class="form-group panel-cont">
							    <label class="col-sm-2 control-label">是否限制影院使用</label>
							    <div class="col-sm-5">
							      <input style="margin-top: 8px" class="isShow" type="checkbox" value="1" />
							    </div>
							    <span class="col-sm-4 tips">
							    	<i class="fa fa-info-circle"></i>
							    	若选中，则可填写具体影院
							    </span>
						  	</div>
							<div class="hide-text">
						  		<div class="form-group panel-cont">
								    <label class="col-sm-2 control-label">请填写待搜索影院名</label>
								    <div class="col-sm-5">
								    	<input type='text' class='form-control' name='' value='' />
								    </div>
							  	</div>
						  		<div class="form-group panel-cont">
								    <label class="col-sm-2 control-label">确定影院名</label>
								    <div class="col-sm-5">
								    	<textarea class="form-control" rows="4" cols="12" readonly="readonly"></textarea>
								    </div>
								    <span class="col-sm-4 tips">
								    	<i class="fa fa-info-circle"></i>
								    	提交影院以此栏内容为准
								    </span>
							  	</div>
						  	</div>
						  	<div class="form-group panel-cont">
							    <label class="col-sm-2 control-label">是否限制城市使用</label>
							    <div class="col-sm-5">
							      <input style="margin-top: 8px" class="isShow" type="checkbox" value="2" />
							    </div>
							    <span class="col-sm-4 tips">
							    	<i class="fa fa-info-circle"></i>
							    	 若选中，则可填写具体城市
							    </span>
						  	</div>
							<div class="hide-text">
						  		<div class="form-group panel-cont">
								    <label class="col-sm-2 control-label">请填写待搜索城市名</label>
								    <div class="col-sm-5">
								    	<input type='text' class='form-control' name='' value='' />
								    </div>
							  	</div>
						  		<div class="form-group panel-cont">
								    <label class="col-sm-2 control-label">确定城市名</label>
								    <div class="col-sm-5">
								    	<textarea class="form-control" rows="4" cols="12" readonly="readonly"></textarea>
								    </div>
								    <span class="col-sm-4 tips">
								    	<i class="fa fa-info-circle"></i>
								    	提交城市以此栏内容为准
								    </span>
							  	</div>
						  	</div> -->
						</div>						
					</div>

				  	
				  	<div class="form-group panel-btn">
				  		<div class="col-sm-2 col-sm-offset-3">
				  			<!-- <button type="button" class="btn btn-green next-btn">下一个</i></button> -->
				  			<button type="button" class="btn btn-danger submit-btn">提交&nbsp;<i class="fa fa-arrow-circle-right"></i></button>
				  		</div>
				  	</div>
				</div>
			</div>
		</div>
	</div>
</form>
<script>
	/**********************************************变量**********************************************/
	var token = MXCookie.get("token",token);  // 登陆token;
	var username = GetQueryString("username"); // 用户名


	
	/**********************************************事件**********************************************/
	datePicker($(".startDate"));  // 开始时间
	datePicker($(".endDate"));    // 结束时间	
	// checked是否被选中
	$(".page-cont").on("click",".isShow",function(){
		var index = $(".isShow").index(this);
		if($(this).is(':checked') == true){
			$(".hide-text").eq(index).show();
		}else{
			$(".hide-text").eq(index).hide();
		}
	});

	// var str1 = $(".body-cont").html();
	// // 下一个
	// $(".next-btn").click(function(){
	// 	$(".panel-btn").before(str1);
	// });
	
	// 删除
	$(".page-cont").on("click",".delete-btn",function(){
		$(this).parent().parent().parent().remove();
	});

	// input值改变
	$("body").on("keyup",".check-film",function(){
		var index = $(".check-film").index(this);
		var filmName = $(".check-film").eq(index).val();
		checkFilm(filmName,index);
	});

	// 提交
	$(".submit-btn").click(function(){
		if(msgCheck()){
			addCoupon();
		}
	});
	/**********************************************函数**********************************************/


	// 信息验证
	function msgCheck(){
		if($(".couponName").val() == ""){
			tips("请填写优惠券名称");
			$(this).focus();
			return false;
		}else if($(".couponIntro").val() == ""){
			tips("请填写优惠券介绍");
			$(this).focus();
			return false;
		}else if($(".startDate").val() == ""){
			tips("请填选择开始时间");
			$(this).focus();
			return false;
		}else if($(".endtDate").val() == ""){
			tips("请填选择结束时间");
			$(this).focus();
			return false;
		}else if($(".maxPrice").val() == ""){
			tips("请填写最大价格");
			$(this).focus();
			return false;
		}else{
			return true;
		}
	}

	// 待搜索影片名
	function checkFilm(filmName,index){
		$(".check-film-list").eq(index).html("");
		$.ajax({
			url: localStorage.grobalUrl + "/discount/NameToId",
			type:"post",
			data:{
				username: GetQueryString("username"),
				token: token,	
				filmName: filmName			
			},
			success: function(data){
				console.log(data);
				if(data.errCode == "00000"){
					$(".check-film-list").eq(index).show();
					var dataVal = data.errMsg;
					var str = '<li></li>';
					for(var i = 0; i < dataVal.length; i ++){
						$(".check-film-list").eq(index).append(str);
						$(".check-film-list:eq("+ index +") li").eq(i).text(dataVal[i].show_name).attr("data-id",dataVal[i].film_id);
						// $(".check-film-list").eq(index).append('<li data-id="'+ dataVal[i].film_id +'">'+ dataVal[i].show_name +'</li>');
					}
					$(".check-film-list").eq(index).find("li").click(function(){
						var filmName = $(this).text();
						var filmId = $(this).attr("data-id");
						$(".check-film-list").hide();
						$(".check-film").eq(index).val("").attr("data-id",filmId).attr("data-name",filmName);
						$(".movieName").append(filmName+"，");
					});
				}else if(data.errCode == "00005"){
					tips("请重新登陆");
					setTimeout(function(){
						window.location.href = "login_login.html";
					},500);					
				}else{}
			},
			error: function(error){
				console.log(error);
			}
		})
	}


	// 新增优惠券
	function addCoupon(){
		$.ajax({
			url: localStorage.grobalUrl + "/discount/addDiscount",
			type: "post",
			data:{
				username: GetQueryString("username"),
				token: token,	
				film_id: $(".check-film").attr("data-id"),
				film_name: $(".check-film").attr("data-name"),
				name: $(".couponName").val(),
				prefix: $(".applyScene").find("option:selected").val(),
				introduction: $(".couponIntro").val(),
				beginDate: $(".startDate").val(),
				endDate: $(".endDate").val(),
				maxPrice: $(".maxPrice").val(),

			},
			success: function(data){
				console.log(data);
				if(data.errCode == "00000"){
					getPage("view/coupon/list.html");
				}else if(data.errCode == "00005"){
					tips("请重新登陆");
					setTimeout(function(){
						window.location.href = "login_login.html";
					},500);					
				}else{
					tips(data.errMsg);
				}
			},
			error: function(error){
				console.log(error);
			}
		});
	}
	
</script>
